/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.capex.javaee.smtp;

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class sendfile {

    public static void main(String[] args) {

        String to = "evegaluna@gmail.com";
        String from = "evegaluna@gmail.com";
        String host = "smtp.gmail.com";
        String filename = "C:\\Users\\Acer\\JAVA.pptx";
        boolean debug = true;
        String msgText1 = "Sending a file.\n";
        String subject = "Sending a file";
        Properties props = System.getProperties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.auth", "true");
        props.put("mail.debug", "true");
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.ssl.enable", "true");
        Session session = Session.getInstance(props, new MailAuthenticator());
        session.setDebug(debug);
        try {
// create a message
            MimeMessage msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(from));

            InternetAddress[] address = {new InternetAddress(to)};
            msg.setRecipients(Message.RecipientType.TO, address);
            msg.setSubject(subject); // create and fill the first message part
            MimeBodyPart mbp1 = new MimeBodyPart();
            mbp1.setText(msgText1);
// create the second message part
            MimeBodyPart mbp2 = new MimeBodyPart(); // attach the file to the message
            FileDataSource fds = new FileDataSource(filename);
            mbp2.setDataHandler(new DataHandler(fds));
            mbp2.setFileName(fds.getName());
// create the Multipart and add its parts to it
            Multipart mp = new MimeMultipart();
            mp.addBodyPart(mbp1);
            mp.addBodyPart(mbp2);
            // add the Multipart to the message
            msg.setContent(mp);
            // set the Date: header
            msg.setSentDate(new Date());
// send the message
            Transport.send(msg);
        } catch (MessagingException mex) {
            mex.printStackTrace();
            Exception ex = null;
            if ((ex = mex.getNextException()) != null) {
                ex.printStackTrace();
            }
        }
    }
}

//class MailAuthenticator extends Authenticator {
//
//    public MailAuthenticator() {
//    }
//
//    @Override
//    public PasswordAuthentication getPasswordAuthentication() {
//        return new PasswordAuthentication("evegaluna@gmail.com", "k2i8k9o7");
//    }
//}
